package com.blb.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.blb_erp.entity.JsonResult;
import com.blb.blb_erp.entity.SRole;
import com.blb.blb_erp.entity.SRoleMenu;
import com.blb.blb_erp.service.ISRoleMenuService;
import com.blb.blb_erp.service.ISRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private ISRoleService roleService;

    @Autowired
    private ISRoleMenuService roleMenuService;

    @GetMapping("/page/{curr}")
    public JsonResult pageRole(@PathVariable("curr")Integer curr){
        try {
            IPage<SRole> pageArg = new Page<>();
            pageArg.setCurrent(curr);
            pageArg.setSize(10);
            IPage<SRole> page = roleService.page(pageArg);
            return new JsonResult(1,page);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PostMapping
    public JsonResult saveRole(@RequestBody SRole role){
        try {
            roleService.save(role);
            return new JsonResult(1, null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PutMapping
    public JsonResult updateRole(@RequestBody SRole role){
        try {
            roleService.updateById(role);
            return new JsonResult(1, null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @DeleteMapping("{roleId}")
    public JsonResult delRole(@PathVariable("roleId") String roleId){
        try {
            roleService.removeById(roleId);
            return new JsonResult(1, null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @PostMapping("{roleId}/{menuId}")
    public JsonResult addRoleMenu(@PathVariable("roleId")Integer roleId,
                                  @PathVariable("menuId")String menuId){
        try {
            SRoleMenu roleMenu = new SRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenuService.save(roleMenu);
            return new JsonResult(1, null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @DeleteMapping("{roleId}/{menuId}")
    public JsonResult deleteRoleMenu(@PathVariable("roleId")Integer roleId,
                                  @PathVariable("menuId")String menuId){
        try {
            roleMenuService.remove(new QueryWrapper<SRoleMenu>().lambda()
                    .eq(SRoleMenu::getMenuId,menuId)
                    .eq(SRoleMenu::getRoleId,roleId));
            return new JsonResult(1, null);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }
}
